package com.flight.trans.adapter;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.flight.trans.R;
import com.flight.trans.bean.LeavePortWarehousingDetailBean.WarehousingDetailData.WarehousingMainInfoBean.MainInfoDetail;
import com.flight.trans.global.Constant;
import com.flight.trans.util.CommonUtils;
import com.flight.trans.view.edit.GoodsEditorActivity;
import com.flight.trans.base.BaseActivity;
import com.flight.trans.base.BaseRecyclerAdapter;
import com.flight.trans.base.BaseViewHolder;

import java.util.List;

/**
 * 货物信息
 */
public class LeavePortWarehousingGoodsAdapter extends BaseRecyclerAdapter<MainInfoDetail> {

    public LeavePortWarehousingGoodsAdapter(Context mContext, List<MainInfoDetail> mList , boolean isEditor) {
        super(mContext, mList);
        this.IsEditor = isEditor ;
    }

    @Override
    public int setContentView() {
        return R.layout.adapter_leave_port_warehousing_goods;
    }

    @Override
    protected void bindData(BaseViewHolder holder, MainInfoDetail data, int position) {
        Button mBtnAddGoods = holder.findViewById(R.id.mBtnAddGoods);
        LinearLayout mLlContainerGoods = holder.findViewById(R.id.mLlContainerGoods);

        TextView mTvPosition = holder.findViewById(R.id.mTvPosition);
        TextView mTvNumber = holder.findViewById(R.id.mTvNumber);
        TextView mTvWeight = holder.findViewById(R.id.mTvWeight);
        TextView mTvVolume = holder.findViewById(R.id.mTvVolume);
        TextView mTvVolumeResolve = holder.findViewById(R.id.mTvVolumeResolve);
        ImageButton mIbDefaultVolume = holder.findViewById(R.id.mIbDefaultVolume);
        ImageButton mIbBridgeWeigh = holder.findViewById(R.id.mIbBridgeWeigh);

        if(data == null){
            mBtnAddGoods.setVisibility(View.VISIBLE);
            mLlContainerGoods.setVisibility(View.GONE);

            if(IsEditor)
                setOnItemClickListener(mBtnAddGoods , position);
        } else {
            mBtnAddGoods.setVisibility(View.GONE);
            mLlContainerGoods.setVisibility(View.VISIBLE);

            mTvPosition.setText((position < 9 ? "0" : "") + (position + 1));

            mTvNumber.setText(String.valueOf(data.getDetailNumber() != 0 ? data.getDetailNumber() : ""));
            mTvWeight.setText(String.valueOf(data.getDetailWeight() != 0 ? data.getDetailWeight() : ""));

            if(data.getDetailLength() != 0 && data.getDetailWidth() != 0 && data.getDetailHeight()!= 0) {
                mTvVolumeResolve.setText(CommonUtils.getDouble(data.getDetailLength() * 100) + " , " + CommonUtils.getDouble(data.getDetailWidth() * 100) + " , " + CommonUtils.getDouble(data.getDetailHeight() * 100));
                mTvVolume.setText(CommonUtils.getDoubleFour(data.getVolume()));
            }else {
                mTvVolumeResolve.setText("");
                mTvVolume.setText("0");
            }

            mIbDefaultVolume.setVisibility(IsEditor ? View.VISIBLE : View.GONE);

            if(IsEditor) {
                // 输入框点击事件
                mTvNumber.setOnClickListener(new EditOnClickListener(data , position));
                mTvWeight.setOnClickListener(new EditOnClickListener(data , position));
                mTvVolumeResolve.setOnClickListener(new EditOnClickListener(data , position));
                // 条目点击事件
                setOnItemLongClickListener(mLlContainerGoods, position);
                // 默认体积
                setOnTextClickListener(mIbDefaultVolume , position);
                // 获取地磅重量
                setOnTextClickListener(mIbBridgeWeigh , position);
            }
        }
    }

    /**
     * 点击事件
     */
    private class EditOnClickListener implements View.OnClickListener{

        private MainInfoDetail data ;
        private int position;

        public EditOnClickListener(MainInfoDetail data , int position){
            this.data = data ;
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            Bundle bundle = new Bundle();
            bundle.putInt(Constant.Count , data.getDetailNumber());
            bundle.putDouble(Constant.Weight , data.getDetailWeight());
            bundle.putDouble(Constant.Length , data.getDetailLength());
            bundle.putDouble(Constant.Width , data.getDetailWidth());
            bundle.putDouble(Constant.Height , data.getDetailHeight());
            bundle.putInt(Constant.Position , position);

            GoodsEditorActivity.openActivity((BaseActivity) mContext,editorCallbackListener, bundle);
        }
    }

    /**
     * 监听键盘
     */
    private GoodsEditorActivity.EditorCallbackListener editorCallbackListener ;

    public void setEditorCallbackListener(GoodsEditorActivity.EditorCallbackListener editorCallbackListener){
        this.editorCallbackListener = editorCallbackListener ;
    }
}
